{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3e199af2",
   "metadata": {},
   "source": [
    "# 1.requests(让HTTPS服务人类)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "65e4d302",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f54ce19c",
   "metadata": {},
   "source": [
    "* r 是指response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "89f5e8a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(\"https://cn.bing.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3c9aefeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c42dc1c9",
   "metadata": {},
   "source": [
    "* r.content\n",
    "> 1.可以获取请求数据内容(观察页面数据形态 HTML，Json)               \n",
    "> 2.b 是指 bytes ： 二进制文件\n",
    "* r.Json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "48b712e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(\"https://httpbin.org/get\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c94521de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate, br\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.26.0\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-62b2f957-4620a5262a3fa927265a3f0b\"\\n  }, \\n  \"origin\": \"120.85.40.126\", \\n  \"url\": \"https://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3dfc96d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate, br',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.26.0',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62b2f957-4620a5262a3fa927265a3f0b'},\n",
       " 'origin': '120.85.40.126',\n",
       " 'url': 'https://httpbin.org/get'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()# 将json数据转换成字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3c924a3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate, br\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.26.0\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-62b2f957-4620a5262a3fa927265a3f0b\"\\n  }, \\n  \"origin\": \"120.85.40.126\", \\n  \"url\": \"https://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5e6f031",
   "metadata": {},
   "source": [
    "* 3. payload\n",
    "> 使用payload来实现请求数据传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dc314c06",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'name':'binger',\n",
    "    'job':'stu'\n",
    "}\n",
    "r = requests.get(\"https://httpbin.org/get\",params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4afa72df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "53c04e2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {'job': 'stu', 'name': 'binger'},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate, br',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.26.0',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62b2f98a-080273153f372a8a1b31bba4'},\n",
       " 'origin': '120.85.40.126',\n",
       " 'url': 'https://httpbin.org/get?name=binger&job=stu'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec47d9f4",
   "metadata": {},
   "source": [
    "# 2. face++ detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bec1199d",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (Temp/ipykernel_31320/2271148228.py, line 10)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\贺雪\\AppData\\Local\\Temp/ipykernel_31320/2271148228.py\"\u001b[1;36m, line \u001b[1;32m10\u001b[0m\n\u001b[1;33m    'return_attributes':'gender,age,smiling,emotion'\u001b[0m\n\u001b[1;37m                       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 1.准备base url (对应该功能的url链接)\n",
    "face_detect_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 2.准备参数数据\n",
    "payload = {\n",
    "    'api_pay':'b0EHP47Q3skXaAdxRUxVSPdsDFpQe93z',\n",
    "    'api_secret':'ep0nGCCMAqSrGeXMx7202O6Xy7MDC1k7',\n",
    "    'image_url':'https://cn.bing.com/images/search?view=detailV2&ccid=W4yVwB7h&id=19DCA44004B81EBA7FE9088BF43EC3E46BB4C3F0&thid=OIP.W4yVwB7hXWNmeANFaVboYAHaFz&mediaurl=https%3a%2f%2fpic1.zhimg.com%2fv2-35306ff8000cc2ad945bb8c04b3a5e0e_b.jpg&exph=514&expw=656&q=%e4%ba%ba%e8%84%b8%e5%9b%be%e7%89%87&simid=608033542482644497&FORM=IRPRST&ck=95B48E2DC1ADACBC3672A748139FA433&selectedIndex=0&idpp=overlayview&ajaxhist=0&ajaxserp=0'\n",
    "    'return_landmark=1'\n",
    "    'return_attributes':'gender,age,smiling,emotion'\n",
    "}\n",
    "# 3.检查调用的请求方法：POST\n",
    "# 4.请求人脸数据\n",
    "r = requests.post(url = face_detect_url,params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4577d032",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4b89d63e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {'job': 'stu', 'name': 'binger'},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate, br',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.26.0',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62b2f98a-080273153f372a8a1b31bba4'},\n",
       " 'origin': '120.85.40.126',\n",
       " 'url': 'https://httpbin.org/get?name=binger&job=stu'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
